package com.gxuwz.endrskyweather.repository;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.gxuwz.endrskyweather.db.WeatherDBHelper;
import com.gxuwz.endrskyweather.model.City;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CityRepository {
    private WeatherDBHelper dbHelper;

    public CityRepository(Context context) {
        this.dbHelper = new WeatherDBHelper(context);
    }

    public List<City> getAllCities() {
        List<City> cities = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        
        Cursor cursor = db.query(
            WeatherDBHelper.TABLE_AREA,
            null, null, null, null, null, null
        );

        Log.d("CityRepository", "Total cities in DB: " + cursor.getCount());
        
        Set<String> uniqueCities = new HashSet<>();
        if (cursor.moveToFirst()) {
            do {
                String cityCn = cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_CITY_CN));
                String provCn = cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_PROV_CN));
                String cityKey = cityCn + "-" + provCn;
                
                if (!uniqueCities.contains(cityKey)) {
                    uniqueCities.add(cityKey);
                    City city = new City(
                        cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_CODE)),
                        cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_ID)),
                        cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_EN)),
                        cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_CN)),
                        cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_CITY_EN)),
                        cityCn,
                        cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_PROV_EN)),
                        provCn
                    );
                    cities.add(city);
                    Log.d("CityRepository", "Added city: " + cityKey);
                } else {
                    Log.d("CityRepository", "Duplicate city skipped: " + cityKey);
                }
            } while (cursor.moveToNext());
        }
        
        cursor.close();
        db.close();
        return cities;
    }

    public List<City> searchCitiesByName(String query) {
        List<City> results = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        
        String selection = WeatherDBHelper.COLUMN_CITY_CN + " LIKE ? OR " + 
                         WeatherDBHelper.COLUMN_AREA_CN + " LIKE ?";
        String[] selectionArgs = new String[]{"%" + query + "%", "%" + query + "%"};
        
        Cursor cursor = db.query(
            WeatherDBHelper.TABLE_AREA,
            null, selection, selectionArgs, null, null, null
        );

        if (cursor.moveToFirst()) {
            do {
                City city = new City(
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_CODE)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_ID)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_EN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_CN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_CITY_EN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_CITY_CN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_PROV_EN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_PROV_CN))
                );
                results.add(city);
            } while (cursor.moveToNext());
        }
        
        cursor.close();
        db.close();
        return results;
    }

    public List<City> searchCitiesByCode(String code) {
        List<City> results = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        
        String selection = WeatherDBHelper.COLUMN_AREA_CODE + " LIKE ?";
        String[] selectionArgs = new String[]{"%" + code + "%"};
        
        Cursor cursor = db.query(
            WeatherDBHelper.TABLE_AREA,
            null, selection, selectionArgs, null, null, null
        );

        if (cursor.moveToFirst()) {
            do {
                City city = new City(
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_CODE)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_ID)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_EN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_AREA_CN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_CITY_EN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_CITY_CN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_PROV_EN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(WeatherDBHelper.COLUMN_PROV_CN))
                );
                results.add(city);
            } while (cursor.moveToNext());
        }
        
        cursor.close();
        db.close();
        return results;
    }
}
